home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / pascal / fielddh.exe / STR_STF.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1992-04-22  |  19.0 KB  |  512 lines

  1. UNIT STR_STF;
  2.   {**    STRING Definition and OPERATIONS  ***}
  3.  
  4. {$O-,F+,D-}
  5.  
  6. INTERFACE
  7. {**************************************************************}
  8. {* Trim   removes leading/trailing blanks.                    *}
  9. {*                                                            *}
  10. {**************************************************************}
  11. FUNCTION TRIM        (Str : string) : string;
  12.  
  13. FUNCTION TRIM_Leading_Only (Str : string) : string;
  14. FUNCTION TRIM_Trailing_Only (Str : string) : string;
  15. FUNCTION TRIM_Quotes (Str : string) : string;
  16.  
  17. {**************************************************************}
  18. {* Right_Justify adds leading blanks.                         *}
  19. {*    NOTE: does not handle cases when                        *}
  20. {*                   Size_To_Be < ACTUAL NUMBER OF CHARACTERS *}
  21. {**************************************************************}
  22. FUNCTION Right_Justify (Str : string; Size_To_Be : integer) : string;
  23.  
  24. {***************************************************************}
  25. {* Center_Str   centers the characters in the string based     *}
  26. {*              upon the size/midpoint specified.              *}
  27. {***************************************************************}
  28. FUNCTION Center_Str (Str : string; Output_Size : integer) : string;
  29.  
  30. {**************************************************************}
  31. {* Change_Case changes the case of the string to UPPER.       *}
  32. {*                                                            *}
  33. {**************************************************************}
  34. FUNCTION CHANGE_CASE (Str : string) : string;
  35. FUNCTION Lower_Case (Str : string) : string;
  36.  
  37. {**************************************************************}
  38. {* Int_To_Str returns the number converted into ascii chars.  *}
  39. {*                                                            *}
  40. {**************************************************************}
  41. FUNCTION Int_To_Str  (Num : LongInt) : string;
  42.  
  43.  
  44. {**************************************************************}
  45. {* Find_Char   returns the position of the char               *}
  46. {*                                                            *}
  47. {**************************************************************}
  48. FUNCTION Find_Char   (Str      : string;
  49.                       Char_Is  : char;
  50.                       Start_At : integer) : INTEGER;
  51.  
  52. {**************************************************************}
  53. {* Delete_The_Char   delete all occurances of the char        *}
  54. {*                                                            *}
  55. {**************************************************************}
  56. FUNCTION Delete_The_Char
  57.                      (Str      : string;
  58.                       Char_Is  : char) : string;
  59.  
  60. {**************************************************************}
  61. {* Replace_Str_Into  inserts the small string into the        *}
  62. {*                   org_str at the position specified        *}
  63. {**************************************************************}
  64. FUNCTION Replace_Str_Into (Org_Str     : String;
  65.                            Small_Str   : string;
  66.                            Start, Stop : integer) : string;
  67.  
  68. {**************************************************************}
  69. {* procedure Get_Word_Around_Position                         *}
  70. {*     returns the word based AROUND the position specified   *}
  71. {*     Searches for blanks around the start_pos               *}
  72. {*        looking left then right.                            *}
  73. {**************************************************************}
  74. function Get_Word_Around_Position
  75.                      (Str                    : string;
  76.                       Start_Pos              : integer;
  77.                       Leftmost_Char_Boundry  : integer;
  78.                       Rightmost_Char_Boundry : integer;
  79.                       VAR Found_Left_Pos     : integer;
  80.                       VAR Found_Word_Size    : integer) : string;
  81.  
  82. {**************************************************************}
  83. {* returns a string with duplicate chars deleted.             *}
  84. {**************************************************************}
  85. function Delete_Duplicate_Chars_In_Str (Str            : string;
  86.                                         Limit_In_A_Row : byte): string;
  87.  
  88. {**************************************************************}
  89. {* returns a string filled with the character specified       *}
  90. {**************************************************************}
  91. function Fill_String(Len : Byte; Ch : Char) : String;
  92.  
  93. {**************************************************************}
  94. {* Truncates a string to a specified length                   *}
  95. {**************************************************************}
  96. function Trunc_Str(TString : String; Len : Byte) : String;
  97.  
  98. {**************************************************************}
  99. {* Pads a string to a specified length with a specified character }
  100. {**************************************************************}
  101. function Pad_Char(PString : String; Ch : Char; Len : Byte) : String;
  102.  
  103.  
  104. {**************************************************************}
  105. {* Left-justify a string within a certain width               *}
  106. {**************************************************************}
  107. function Left_Justify_Str (S : String; Width : Byte) : String;
  108.  
  109. {**************************************************************}
  110. {**************************************************************}
  111. {**************************************************************}
  112. IMPLEMENTATION
  113.  
  114. {**************************************************************************}
  115. function Min(N1, N2 : Longint) : Longint;
  116. { Returns the smaller of two numbers }
  117. begin
  118.   if N1 <= N2 then
  119.     Min := N1
  120.   else
  121.     Min := N2;
  122. end; { Min }
  123.  
  124. {**************************************************************************}
  125. function Max(N1, N2 : Longint) : Longint;
  126. { Returns the larger of two numbers }
  127. begin
  128.   if N1 >= N2 then
  129.     Max := N1
  130.   else
  131.     Max := N2;
  132. end; { Max }
  133.  
  134. {**************************************************************}
  135. {* returns a string filled with the character specified       *}
  136. {**************************************************************}
  137. function Fill_String(Len : Byte; Ch : Char) : String;
  138. var
  139.   S : String;
  140. begin
  141.   IF (Len > 0) THEN
  142.     BEGIN
  143.       S[0] := Chr(Len);
  144.       FillChar(S[1], Len, Ch);
  145.       Fill_String := S;
  146.     END
  147.   ELSE Fill_String := '';
  148. end; { FillString }
  149.  
  150. {**************************************************************}
  151. {* Truncates a string to a specified length                   *}
  152. {**************************************************************}
  153. function Trunc_Str(TString : String; Len : Byte) : String;
  154. begin
  155.   if (Length(TString) > Len) then
  156.     begin
  157.       {Delete(TString, Succ(Len), Length(TString) - Len);}
  158.       {Move(TString[Succ(Len)+(LENGTH(TString)-Len)], TString[Succ(Len)],
  159.            Succ(Length(TString)) - Succ(Len) - Length(TString) - Len));}
  160.       Move(TString[LENGTH(TString)+1], TString[Succ(Len)], 2*Len);
  161.       Dec(TString[0], Length(TString) - Len);
  162.     end;
  163.   Str_Stf.Trunc_Str := TString;
  164. end; { TruncStr }
  165.  
  166. {**************************************************************}
  167. {* Pads a string to a specified length with a specified character }
  168. {**************************************************************}
  169. function Pad_Char(PString : String; Ch : Char; Len : Byte) : String;
  170. var
  171.   CurrLen : Byte;
  172. begin
  173.   CurrLen := Min(Length(PString), Len);
  174.   PString[0] := Chr(Len);
  175.   FillChar(PString[Succ(CurrLen)], Len - CurrLen, Ch);
  176.   Pad_Char := PString;
  177. end; { PadChar }
  178.  
  179. {**************************************************************}
  180. {* Left-justify a string within a certain width               *}
  181. {**************************************************************}
  182. function Left_Justify_Str(S : String; Width : Byte) : String;
  183. begin
  184.   Left_Justify_Str := Str_Stf.Pad_Char(S, ' ', Width);
  185. end; { Left_Justify_Str }
  186.  
  187. {**************************************************************}
  188. {* Trim   removes leading/trailing blanks.                    *}
  189. {*                                                            *}
  190. {**************************************************************}
  191. FUNCTION TRIM (Str : string) : string;
  192. VAR
  193.   i : integer;
  194. BEGIN
  195.   i := 1;
  196.   WHILE ((i < LENGTH(Str)) and (Str[i] = ' '))
  197.     DO INC(i);
  198.  
  199.   IF (i > 1) THEN
  200.     BEGIN
  201.       {Str := COPY (Str, i, Length(Str));}
  202.       Move (Str[i], Str[1], Succ(LENGTH(Str))-i);
  203.       DEC (Str[0], pred(i));
  204.     END;
  205.  
  206.   WHILE (Str[LENGTH(str)] = ' ')
  207.     DO DEC (Str[0]);
  208.  
  209.   Trim := Str;
  210. END;  {trim}
  211.  
  212. {**************************************************************}
  213. {* Trim_Lead   removes leading blanks.                        *}
  214. {*                                                            *}
  215. {**************************************************************}
  216. FUNCTION TRIM_Leading_Only (Str : string) : string;
  217. VAR
  218.   i : integer;
  219. BEGIN
  220.   i := 1;
  221.   WHILE ((i < LENGTH(Str)) and (Str[i] = ' '))
  222.     DO INC(i);
  223.  
  224.   IF (i > 1) THEN
  225.     BEGIN
  226.       {Str := COPY (Str, i, Length(Str));}
  227.       Move (Str[i], Str[1], Succ(LENGTH(Str))-i);
  228.       DEC (Str[0], pred(i));
  229.     END;
  230.  
  231.   Trim_Leading_Only := Str;
  232. END;  {trim_leading_Only}
  233.  
  234. {***************************************************************}
  235. FUNCTION TRIM_Trailing_Only (Str : string) : string;
  236. VAR
  237.   i : integer;
  238. BEGIN
  239.   WHILE (Str[LENGTH(str)] = ' ')
  240.     DO DEC (Str[0]);
  241.  
  242.   Trim_Trailing_Only := Str;
  243. END;  {trim}
  244.  
  245. {***************************************************************}
  246. {*------------------------------------------------------*}
  247. {* Trim off any lead/trail quotes!                      *}
  248. {*------------------------------------------------------*}
  249. FUNCTION TRIM_Quotes (Str : string) : string;
  250. var
  251.   Token_Pos : byte;
  252. begin
  253.   Token_Pos := POS ('"', Str);
  254.   IF (Token_Pos = 1) THEN  {was > 0, 02-21-92}
  255.     BEGIN
  256.       Str  := COPY (Str, 2 {Token_Pos + 1}, LENGTH(Str));
  257.       {Token_Pos := POS ('"', Str);}
  258.       Token_Pos := LENGTH(Str);
  259.       IF (Str[Token_Pos] = '"')
  260.         THEN Str := COPY (Str, 1, (Token_Pos - 1));
  261.     END; {if}
  262. Trim_Quotes := Str;
  263. end; {Trim_Quotes}
  264.  
  265. {***************************************************************}
  266. {* Right_Justify adds leading blanks.                          *}
  267. {*    NOTE: does not handle cases when                         *}
  268. {*                    Size_To_Be < ACTUAL NUMBER OF CHARACTERS *}
  269. {***************************************************************}
  270. FUNCTION Right_Justify (Str : string; Size_To_Be : integer) : string;
  271. VAR
  272.   Temp_Str  : string;
  273. BEGIN
  274.   Temp_Str := TRIM (Str);   {to assure proper length--and NON-BLANK}
  275.   Right_Justify := Str_Stf.Left_Justify_Str
  276.                                ('', Size_To_Be - Length(Str)) + Str;
  277.  
  278. {  WHILE ((LENGTH(Temp_Str) > 0) AND
  279.          ( (Size_To_Be > LENGTH (Temp_Str)) OR
  280.            (Temp_Str[Size_To_Be] = ' ') ) )
  281.     DO Temp_Str := ' '+ COPY (Temp_Str, 1, Size_To_Be-1);
  282.   Right_Justify := Temp_Str;}
  283.  
  284. END; {right_justify}
  285.  
  286. {***************************************************************}
  287. {* Center_Str   centers the characters in the string based     *}
  288. {*              upon the size/midpoint specified.              *}
  289. {***************************************************************}
  290. FUNCTION Center_Str (Str : string; Output_Size : integer) : string;
  291. VAR
  292.   Ret_Str : string;
  293.   Size    : integer;
  294. BEGIN
  295.   { blank out returning string}
  296.   Ret_Str := Str_Stf.Fill_String(Output_Size, ' ');
  297.   {FillChar (Ret_Str, output_size, ' ');
  298.    Ret_Str[0] := chr(Output_Size);}
  299.  
  300.   Str := TRIM (Str);
  301.   Size := LENGTH (Str);
  302.   IF (Output_Size <= Size)
  303.     THEN Ret_Str := Str
  304.   ELSE
  305.     BEGIN
  306.       Insert (Str, Ret_Str, (((Output_Size - Size) div 2)+1));
  307.       Ret_Str := COPY (Ret_Str, 1, OutPut_Size);
  308.     END;
  309.   Center_Str := Ret_Str;
  310. END; {center_str}
  311.  
  312. {**************************************************************}
  313. {* Change_Case changes the case of the string to UPPER.       *}
  314. {*                                                            *}
  315. {**************************************************************}
  316. FUNCTION Change_Case (Str : string) : string;
  317. var
  318.   i : integer;
  319. BEGIN
  320.   for i := 1 to LENGTH (Str)
  321.     do Str[i] := UpCase(Str[i]);
  322.   Change_Case := Str;
  323. END;  {change_case}
  324.  
  325. {**************************************************************}
  326. FUNCTION Lower_Case (Str : string) : string;
  327. var
  328.   i : integer;
  329. BEGIN
  330.   for i := 1 to LENGTH (Str)
  331.     do IF ((ORD (Str[i]) >= 65) and (ORD(Str[i]) <= 90))
  332.          THEN Str[i] := CHR(ORD(Str[i])+32);
  333.   Lower_Case := Str;
  334. END;  {lower_case}
  335.  
  336. {**************************************************************}
  337. {* Int_To_Str returns the number converted into ascii chars.  *}
  338. {*                                                            *}
  339. {**************************************************************}
  340. FUNCTION Int_To_Str  (Num : LongInt) : string;
  341. var
  342.   Temp_Str : string;
  343. BEGIN
  344.   STR(Num, Temp_Str);
  345.   Int_To_Str := STR_STF.Trim(Temp_Str);   {left-justify}
  346. END; {int_to_str}
  347.  
  348. {**************************************************************}
  349. {* Find_Char   returns the position of the char               *}
  350. {*                                                            *}
  351. {**************************************************************}
  352. FUNCTION Find_Char (Str      : string;
  353.                     Char_Is  : char;
  354.                     Start_At : integer) : INTEGER;
  355. VAR
  356.   Loc : integer;
  357. BEGIN
  358.   Loc := POS (Char_Is, COPY(Str, Start_At, LENGTH(STR)));
  359.   IF (Loc <> 0)
  360.     THEN Loc := Loc + Start_At -1;
  361.   Find_Char := Loc;
  362. END; {function Find_Char}
  363.  
  364. {**************************************************************}
  365. {* Delete_The_Char   delete all occurances of the char        *}
  366. {*                                                            *}
  367. {**************************************************************}
  368. FUNCTION Delete_The_Char (Str      : string;
  369.                           Char_Is  : char) : string;
  370. VAR
  371.   Loc : integer;
  372. BEGIN
  373.   Loc := 0;
  374.   REPEAT
  375.     Loc := POS (Char_Is, Str);
  376.     IF (Loc <> 0)
  377.       THEN DELETE (Str, Loc, 1);
  378.   UNTIL (Loc = 0);
  379.  
  380.   Delete_The_Char := STR;
  381. END; {function Delete_The_Char}
  382.  
  383. {**************************************************************}
  384. {* Replace_Str_Into  inserts the small string into the        *}
  385. {*                   org_str at the position specified        *}
  386. {**************************************************************}
  387. FUNCTION Replace_Str_Into (Org_Str     : String;
  388.                            Small_Str   : string;
  389.                            Start, Stop : integer) : string;
  390. var
  391.   Temp_Small_Str : string;
  392. begin
  393.   IF (Start = 0)
  394.     THEN Start := 1;
  395.  
  396.   IF (LENGTH(Small_Str) >= (Stop-Start+1))
  397.     THEN Temp_Small_Str := Small_Str
  398.   ELSE Temp_Small_Str := Small_Str +
  399.                        Fill_String ( (Stop-Start+1-LENGTH(Small_Str)), ' ');
  400.   IF (Start > 1)
  401.     THEN Replace_Str_Into := Copy (Org_Str, 1, (Start -1)) +
  402.                              Copy (Temp_Small_Str, 1, (Stop-Start+1))+
  403.                              Copy (Org_Str, (Stop+1) , LENGTH(Org_Str))
  404.     ELSE Replace_Str_Into := Copy (Temp_Small_Str, 1, (Stop-Start+1)) +
  405.                              Copy (Org_Str, Stop+1, LENGTH(Org_Str));
  406. end; {Replace_Str_into}
  407.  
  408. {**************************************************************}
  409. {* procedure Get_Word_Around_Position                         *}
  410. {*     returns the word based AROUND the position specified   *}
  411. {*     Searches for blanks around the start_pos               *}
  412. {*        looking left then right.                            *}
  413. {**************************************************************}
  414. function Get_Word_Around_Position
  415.                                (Str                    : string;
  416.                                 Start_Pos              : integer;
  417.                                 Leftmost_Char_Boundry  : integer;
  418.                                 Rightmost_Char_Boundry : integer;
  419.                                 VAR Found_Left_Pos     : integer;
  420.                                 VAR Found_Word_Size    : integer) : string;
  421. var
  422.   adjust         : integer;
  423.   i              : integer;
  424.   left           : integer;
  425.   line_upper     : string;
  426.   line_size      : integer;
  427.   line_days      : string;
  428.   loc            : integer;
  429.   loc_last       : integer;
  430.   size           : integer;
  431.  
  432. begin
  433.   Get_Word_Around_Position := ' ';
  434.   Found_Left_Pos           := 0;
  435.   Found_Word_Size          := 0;
  436.  
  437.   if (Str[Start_Pos] <> ' ') then
  438.     begin
  439.       {************************************************}
  440.       {*  FIRST: find left-most position              *}
  441.       {************************************************}
  442.       adjust := Start_Pos -1;
  443.       while ((adjust >= leftmost_char_boundry) and
  444.              (Str[adjust] <> ' '))
  445.         do adjust := adjust - 1;
  446.       if ((adjust = leftmost_char_boundry) and (Str[adjust] <> ' '))
  447.         then Found_Left_Pos := adjust
  448.     else Found_Left_Pos := adjust +1;
  449.  
  450.       {************************************************}
  451.       {*  find right-most position                    *}
  452.       {************************************************}
  453.       adjust := Start_Pos +1;
  454.       while ((adjust <= Rightmost_Char_Boundry) and
  455.               (Str[adjust] <> ' '))
  456.         do adjust := adjust + 1;
  457.  
  458.       if ((adjust = Rightmost_char_boundry) and (Str[adjust] <> ' '))
  459.         then Found_Word_Size := adjust - Found_Left_Pos +1
  460.     else Found_Word_Size := adjust - Found_Left_Pos;
  461.  
  462.       Get_Word_Around_Position := Copy (Str, Found_Left_Pos, Found_Word_Size);
  463.  
  464.     end; {if}
  465.  
  466. end; {get_word_around_position}
  467.  
  468. {**************************************************************}
  469. {* returns a string with duplicate chars deleted.             *}
  470. {**************************************************************}
  471. function Delete_Duplicate_Chars_In_Str (Str            : string;
  472.                                         Limit_In_A_Row : byte) : string;
  473. var
  474.   Curr_Pos       : integer;
  475.   i              : integer;
  476.   Same_Chars     : boolean;
  477.   Temp_Str       : string;
  478. begin
  479.   Temp_Str        := Str;
  480.   Limit_In_A_Row  := 5;
  481.   Curr_Pos        := 1;
  482.  
  483.   WHILE ( ((Curr_Pos+Limit_In_A_Row <= LENGTH(Temp_Str))) and
  484.           ((LENGTH(Temp_Str) >= Limit_In_A_Row)) ) DO
  485.     BEGIN
  486.  
  487.       i := Curr_Pos+1;
  488.       Same_Chars := TRUE;
  489.       WHILE ((Same_Chars) and (i <= (Curr_Pos+Limit_In_A_Row-1))) DO
  490.         BEGIN
  491.           IF (Temp_Str[Curr_Pos] <> Temp_Str[i])
  492.             THEN Same_Chars := FALSE
  493.             ELSE INC(i);
  494.         END; {while}
  495.  
  496.       IF (Same_Chars) THEN
  497.         BEGIN
  498.           IF (Curr_Pos = 1)
  499.             THEN Temp_Str := COPY (Temp_Str, Limit_In_A_Row, LENGTH(Temp_Str))
  500.             ELSE Temp_Str := COPY (Temp_Str, 1, Curr_Pos-1) +
  501.                              COPY (Temp_Str, (Curr_Pos+Limit_In_A_Row-1),
  502.                                              LENGTH(Temp_Str));
  503.         END
  504.       ELSE IF (Temp_Str[Curr_Pos+1] <> Temp_Str[Curr_Pos+2])   {*look-ahead*}
  505.              THEN INC (Curr_Pos,2)
  506.              ELSE INC (Curr_Pos);
  507.     END; {while}
  508.  
  509.   Delete_Duplicate_Chars_In_Str := Temp_Str;
  510. end; {delete_duplicate_chars_in_str}
  511.  
  512. END. {unit str_stf}